package com.common.baselibrary;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class BaseTitleView extends FrameLayout {

    private LinearLayout ll_back;
    private ImageView btn_back;
    private TextView title_text;
    private TextView tvRight;
    private LinearLayout ll_right_img;
    private ImageView btn_right;
    private LinearLayout ll_fish;

    private String baseTitleText;
    private String baseRightContent;
    private int baseLeftIcon;
    private int baseRightIcon;
    private boolean baseRightIsText = false;

    private OnClickLeftBackListener onClickLeftBackListener;
    private OnClickRightTextListener onClickRightTextListener;
    private OnclickRightIconListerner onclickRightIconListerner;

    public void setOnClickRightTextListener(OnClickRightTextListener onClickRightTextListener) {
        this.onClickRightTextListener = onClickRightTextListener;
    }

    public void setOnClickLeftBackListener(OnClickLeftBackListener onClickLeftBackListener) {
        this.onClickLeftBackListener = onClickLeftBackListener;
    }

    public BaseTitleView(@NonNull Context context) {
        super(context);
        initView(context);
    }

    public BaseTitleView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public BaseTitleView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initData();

        initView(context);

        TypedArray typedArray =
                context.obtainStyledAttributes(attrs, R.styleable.BaseTitleView, defStyleAttr, 0);

        baseTitleText = typedArray.getString(R.styleable.BaseTitleView_baseTitleText);
        baseRightContent = typedArray.getString(R.styleable.BaseTitleView_baseRightContent);
        baseLeftIcon = typedArray.getResourceId(R.styleable.BaseTitleView_baseLeftIcon, R.drawable.left_black_icon);
        baseRightIcon = typedArray.getResourceId(R.styleable.BaseTitleView_baseRightIcon, R.drawable.icon_message_search);
        baseRightIsText = typedArray.getBoolean(R.styleable.BaseTitleView_baseRightIsText, false);

        setBaseTitleText(baseTitleText);
        setLeftIcon(baseLeftIcon);
        setBaseRightIcon(baseRightIcon);
        isVisbleText(baseRightIsText);
        setRightText(baseRightContent);

    }

    private void initData() {
        baseTitleText = "app共用title";
        baseRightContent = "app右边显示内容";
        baseLeftIcon = R.drawable.left_black_icon;
        baseRightIcon = R.drawable.icon_message_search;
        baseRightIsText = false;
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.baserighttitle, this);
        ll_back = view.findViewById(R.id.ll_back);
        btn_back = view.findViewById(R.id.btn_back);
        title_text = view.findViewById(R.id.title_text);
        ll_right_img = view.findViewById(R.id.ll_right_img);
        btn_right = view.findViewById(R.id.btn_right);
        ll_fish = view.findViewById(R.id.ll_fish);
        tvRight = view.findViewById(R.id.tvRight);
        setBackListener();
//        setRightTextListener();
//        setRightIconListener();

    }

    /**
     * 设置标题内容
     *
     * @param titleText
     */
    private void setBaseTitleText(String titleText) {
        title_text.setText(titleText);
    }

    /**
     * 设置左边图标
     */
    private void setLeftIcon(int imgRes) {
        btn_back.setImageResource(imgRes);
    }

    /**
     * 设置右边图标
     */
    private void setBaseRightIcon(int imgRes) {
        btn_right.setImageResource(imgRes);
    }

    /**
     * 显示右边显示图标还是文字
     */

    private void isVisbleText(boolean isVisble) {
        if (isVisble) {
            ll_fish.setVisibility(VISIBLE);
            ll_right_img.setVisibility(GONE);
        } else {
            ll_fish.setVisibility(GONE);
            ll_right_img.setVisibility(VISIBLE);
        }
    }

    private void setRightText(String rightText) {
        tvRight.setText(rightText);
    }

    /**
     * 左边返回键监听
     */
    private void setBackListener() {
        ll_back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickLeftBackListener != null) {
                    onClickLeftBackListener.onBack();
                }
            }
        });
    }

    /**
     * 右边文字监听
     */

    public void setRightTextListener() {
        ll_fish.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickRightTextListener != null) {
                    onClickRightTextListener.onClickRightText();
                }
            }
        });
    }

    /**
     * 右边图标监听
     */

    public void setRightIconListener() {
        ll_right_img.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onclickRightIconListerner != null) {
                    onclickRightIconListerner.onClickRightIcon();
                }
            }
        });
    }

    public interface OnClickLeftBackListener {
        void onBack();
    }

    public interface OnClickRightTextListener {
        void onClickRightText();
    }

    public interface OnclickRightIconListerner {
        void onClickRightIcon();
    }

}
